package com.zdp.seckill.controller;

import cn.hutool.core.codec.Base64Encoder;
import com.zdp.seckill.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;

/**
 * @author sesshomaru
 * @date 2021/6/17 20:21
 */
@Slf4j
@Controller
public class BaseController {

    public static final String CONTENT_TYPE_FORMED = "application/x-www-form-urlencoded";

    public HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    public String EncodeByMd5(String str) {
        String newstr = null;
        try {
            //确定计算方法
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            Base64Encoder base64en = new Base64Encoder();
            //加密字符串
            newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("密码加密失败");
        }
        return newstr;
    }
}
